package com.niit.mall.mall.auth.config;



import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;

import java.security.KeyPair;

/**
 * @BelongsProject: pardo_mall_backend
 * @BelongsPackage: com.niit.mall.auth.config
 * @Author: ChuXuan
 * @CreateTime: 2025-06-03  08:30
 * @Description: Security配置
 * @Version: 1.0
 */

@Configuration
@EnableConfigurationProperties(JwtProperties.class)
public class SecurityConfig {
	
	@Bean
	public PasswordEncoder passwordEncoder(){
		return new BCryptPasswordEncoder();
	}
	
	/**
	 * 获取密钥对的方法。
	 *
	 * @param properties Jwt属性配置类，包含密钥对所需的配置信息
	 * @return 从指定位置读取的密钥对
	 */
	@Bean
	public KeyPair keyPair(JwtProperties properties){
		// 获取秘钥工厂
		KeyStoreKeyFactory keyStoreKeyFactory =
				new KeyStoreKeyFactory(
						properties.getLocation(),
						properties.getPassword().toCharArray());
		//读取钥匙对
		return keyStoreKeyFactory.getKeyPair(
				properties.getAlias(),
				properties.getPassword().toCharArray());
	}
	
	
}